<template>
	<view class="wrap">
		<!-- 用户信息设置 -->
		<view class="section">
			<view class="section-title">账号设置</view>
			<view class="settings-list">
				<view class="settings-item" @tap="editUserInfo">
					<view class="left">
						<u-icon name="account" size="40" color="#666"></u-icon>
						<text>个人信息</text>
					</view>
					<view class="right">
						<text class="info">{{userInfo.nickName || '未设置'}}</text>
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
				<view class="settings-item" @tap="editPhone">
					<view class="left">
						<u-icon name="phone" size="40" color="#666"></u-icon>
						<text>手机号码</text>
					</view>
					<view class="right">
						<text class="info">{{formatPhone(userInfo.phone) || '未绑定'}}</text>
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
				<view class="settings-item" @tap="manageAddress">
					<view class="left">
						<u-icon name="map" size="40" color="#666"></u-icon>
						<text>收货地址</text>
					</view>
					<view class="right">
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- 通知设置 -->
		<view class="section">
			<view class="section-title">通知设置</view>
			<view class="settings-list">
				<view class="settings-item">
					<view class="left">
						<u-icon name="bell" size="40" color="#666"></u-icon>
						<text>接收订单通知</text>
					</view>
					<view class="right">
						<u-switch v-model="settings.orderNotify" active-color="#EE2F37"></u-switch>
					</view>
				</view>
				<view class="settings-item">
					<view class="left">
						<u-icon name="gift" size="40" color="#666"></u-icon>
						<text>优惠促销通知</text>
					</view>
					<view class="right">
						<u-switch v-model="settings.promoNotify" active-color="#EE2F37"></u-switch>
					</view>
				</view>
			</view>
		</view>

		<!-- 其他设置 -->
		<view class="section">
			<view class="section-title">其他设置</view>
			<view class="settings-list">
				<view class="settings-item" @tap="clearCache">
					<view class="left">
						<u-icon name="trash" size="40" color="#666"></u-icon>
						<text>清除缓存</text>
					</view>
					<view class="right">
						<text class="info">{{cacheSize}}MB</text>
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
				<view class="settings-item" @tap="checkUpdate">
					<view class="left">
						<u-icon name="reload" size="40" color="#666"></u-icon>
						<text>检查更新</text>
					</view>
					<view class="right">
						<text class="info">当前版本 {{version}}</text>
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
				<view class="settings-item" @tap="aboutUs">
					<view class="left">
						<u-icon name="info-circle" size="40" color="#666"></u-icon>
						<text>关于我们</text>
					</view>
					<view class="right">
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- 退出登录按钮 -->
		<view class="logout-btn" @tap="handleLogout" v-if="isLoggedIn">
			退出登录
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},
				settings: {
					orderNotify: true,
					promoNotify: true
				},
				cacheSize: '0.5',
				version: '1.0.0',
				isLoggedIn: true
			}
		},
		onLoad() {
			this.getUserInfo();
		},
		methods: {
			// 获取用户信息
			getUserInfo() {
				const userInfo = uni.getStorageSync('userInfo');
				if (userInfo) {
					this.userInfo = JSON.parse(userInfo);
					this.isLoggedIn = true;
				}
			},
			// 格式化手机号
			formatPhone(phone) {
				if (!phone) return '';
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
			},
			// 编辑个人信息
			editUserInfo() {
				uni.navigateTo({
					url: '/pages/settings/userInfo'
				});
			},
			// 编辑手机号
			editPhone() {
				uni.navigateTo({
					url: '/pages/settings/phoneManage'
				});
			},
			// 管理收货地址
			manageAddress() {
				uni.navigateTo({
					url: '/pages/address/addresslist'
				});
			},
			// 清除缓存
			clearCache() {
				uni.showModal({
					title: '提示',
					content: '确定要清除缓存吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '清理中...'
							});
							setTimeout(() => {
								uni.hideLoading();
								this.cacheSize = '0.0';
								uni.showToast({
									title: '清除成功',
									icon: 'success'
								});
							}, 1000);
						}
					}
				});
			},
			// 检查更新
			checkUpdate() {
				uni.showLoading({
					title: '检查中...'
				});
				setTimeout(() => {
					uni.hideLoading();
					uni.showToast({
						title: '已是最新版本',
						icon: 'none'
					});
				}, 1000);
			},
			// 关于我们
			aboutUs() {
				uni.showModal({
					title: '关于我们',
					content: '私房菜餐厅订餐系统 v1.0.0\n感谢您的使用！',
					showCancel: false
				});
			},
			// 退出登录
			handleLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							uni.clearStorageSync();
							this.userInfo = {};
							this.isLoggedIn = false;
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.wrap {
		min-height: 100vh;
		background: #f5f5f5;
		padding: 20rpx;

		.section {
			margin-bottom: 20rpx;

			.section-title {
				font-size: 28rpx;
				color: #999;
				padding: 20rpx 10rpx;
			}

			.settings-list {
				background: #fff;
				border-radius: 16rpx;
				overflow: hidden;

				.settings-item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 30rpx;
					border-bottom: 1rpx solid #f5f5f5;

					&:last-child {
						border-bottom: none;
					}

					&:active {
						background-color: #f9f9f9;
					}

					.left {
						display: flex;
						align-items: center;
						gap: 20rpx;

						text {
							font-size: 28rpx;
							color: #333;
						}
					}

					.right {
						display: flex;
						align-items: center;
						gap: 10rpx;

						.info {
							font-size: 26rpx;
							color: #999;
							margin-right: 10rpx;
						}
					}
				}
			}
		}

		.logout-btn {
			margin: 60rpx 30rpx;
			background: #EE2F37;
			color: #fff;
			text-align: center;
			padding: 24rpx;
			border-radius: 40rpx;
			font-size: 32rpx;
			font-weight: bold;

			&:active {
				opacity: 0.9;
			}
		}
	}
</style>